package com.sml.contorller;

import com.sml.pojo.Result;
import com.sml.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {

    /**
     * 文件上传
     *
     * @param file 上传的文件
     * @return 文件访问地址
     * @throws IOException IO异常
     */
    @PostMapping("/upload")
    public Result<String> uploadFile(MultipartFile file) throws Exception {
        //需要把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件名字是唯一的，从而防止文件覆盖
        String fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        //存储上传文件到本地
//        file.transferTo(new File("/Users/simpleli/workspace/sml/sring-code/big-event/doc/file/" + fileName));
        //存储上传文件到阿里OSS
        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());
        // 处理文件上传逻辑
        return Result.success(url);
    }
}
